import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:fsharing/core/models/store_total.dart';
import 'package:fsharing/core/viewmodels/pages/mine/manage/store_manage_model.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';
import 'package:fsharing/main.dart';
import 'package:fsharing/ui/pages/mine/order/order_page.dart';
import 'package:fsharing/ui/widgets/background_container.dart';
import 'package:fsharing/ui/widgets/mine_func_button.dart';
import 'package:fsharing/ui/widgets/mine_order_button.dart';
import 'package:fsharing/ui/widgets/round_back_button.dart';
import 'package:provider/provider.dart';

class StoreManagePage extends StatelessWidget {
  final int storeId;

  StoreManagePage({
    Key key,
    this.storeId,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => StoreManageModel(Provider.of(context, listen: false), storeId),
      child: BackgroundContainer(
        child: Scaffold(
          appBar: AppBar(
            leading: RoundBackButton(),
            title: Text('店铺管理'),
            backgroundColor: Colors.black38,
          ),
          body: Consumer<StoreManageModel>(
            builder: (context, model, child) {
              switch (model.viewState) {
                case ViewState.busy:
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                case ViewState.error:
                  return Center(
                    child: Text(model.errorMessage),
                  );
                case ViewState.success:
                  return _BodyWidget();
                default:
                  return Container();
              }
            },
          ),
        ),
      ),
    );
  }
}

class _BodyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Theme(
      data: lightTheme(),
      child: Material(
        child: CustomScrollView(
          slivers: <Widget>[
            _HeaderWidget(),
            _BottomTitleWidget(),
            _BottomFuncWidget(),
          ],
        ),
      ),
    );
  }
}

class _HeaderWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SliverToBoxAdapter(
      child: Theme(
        data: dartTheme(),
        child: Material(
          color: Colors.transparent,
          child: Stack(
            alignment: Alignment.bottomCenter,
            children: <Widget>[
              Container(
                decoration: BoxDecoration(
                  color: Color(0xFF565862),
                ),
                height: 212,
                margin: EdgeInsets.only(bottom: 23),
              ),
              Container(
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/mine/img_header.png'),
                    fit: BoxFit.fill,
                  ),
                ),
                height: 200,
                margin: EdgeInsets.symmetric(horizontal: 18),
              ),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 36),
                height: 212 - 6.0,
                margin: EdgeInsets.only(bottom: 23),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    _HeaderItemOneWidget(),
                    // _HeaderItemTwoWidget(),
                    _HeaderItemThreeWidget(),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class _HeaderItemOneWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    StoreManageModel model = Provider.of(context, listen: false);
    StoreTotal storeTotal = model.storeTotal;
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.end,
      children: <Widget>[
        CircleAvatar(
          radius: 35,
          backgroundImage: CachedNetworkImageProvider(storeTotal.logoUrl ?? ''),
        ),
        Expanded(
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 6),
            child: Text(
              '${storeTotal.storeName}  ',
              style: TextStyle(
                color: Theme.of(context).accentColor,
                fontSize: 25,
                fontWeight: FontWeight.bold,
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ),
        Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/mine/img_auth.png'),
              fit: BoxFit.fill,
            ),
          ),
          padding: EdgeInsets.symmetric(vertical: 4, horizontal: 7),
          child: Text(
            '${storeTotal.businessName ?? '--'}：${storeTotal.businessLevel ?? '--'}',
            style: TextStyle(fontSize: 12),
          ),
        )
      ],
    );
  }
}

class _HeaderItemThreeWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    StoreManageModel model = Provider.of(context, listen: false);
    StoreTotal storeTotal = model.storeTotal;
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        MineOrderButton(
          name: 'assets/images/mine/ic_pending_pay.png',
          title: '待付款',
          badge: storeTotal.total('PAYING_ORDER_TOTAL'),
          onTap: () {
            _onOrderClicked(context, 0);
          },
        ),
        MineOrderButton(
          name: 'assets/images/mine/ic_pending_send.png',
          title: '待发货',
          badge: storeTotal.total('SENDING_ORDER_TOTAL'),
          onTap: () {
            _onOrderClicked(context, 1);
          },
        ),
        MineOrderButton(
          name: 'assets/images/mine/ic_pending_receive.png',
          title: '待收货',
          badge: storeTotal.total('RECEIPTING_ORDER_TOTAL'),
          onTap: () {
            _onOrderClicked(context, 2);
          },
        ),
        MineOrderButton(
          name: 'assets/images/mine/ic_pending_all.png',
          title: '全部订单',
          onTap: () {
            _onOrderClicked(context, 3);
          },
        ),
      ],
    );
  }

  void _onOrderClicked(BuildContext context, int index) {
    StoreManageModel model = Provider.of(context, listen: false);
    final storeId = model.storeId;
    Navigator.of(context).pushNamed('/order', arguments: OrderPageArgs(storeId, index));
  }
}

class _BottomTitleWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SliverToBoxAdapter(
      child: Container(
        margin: EdgeInsets.only(left: 18, top: 22, right: 18, bottom: 4),
        child: Text(
          '常用功能',
          style: TextStyle(
            color: Colors.black87,
            fontSize: 15,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }
}

class _BottomFuncWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    StoreManageModel model = Provider.of(context, listen: false);
    return SliverPadding(
      padding: EdgeInsets.symmetric(horizontal: 12),
      sliver: SliverGrid.extent(
        maxCrossAxisExtent: (375 - 24) / 3,
        childAspectRatio: 1.125,
        children: <Widget>[
          MineFuncButton(
            name: 'assets/images/store/ic_func_bank.png',
            title: '银行账户',
            onTap: () => Navigator.of(context).pushNamed('/bank'),
          ),
          MineFuncButton(
            name: 'assets/images/store/ic_func_business.png',
            title: '业务申请',
            onTap: () => Navigator.of(context).pushNamed('/business/type'),
          ),
          MineFuncButton(
            name: 'assets/images/store/ic_func_withdraw.png',
            title: '提现申请',
            onTap: () => Navigator.of(context).pushNamed('/withdraw', arguments: model.storeId),
          ),
          MineFuncButton(
            name: 'assets/images/store/ic_func_score.png',
            title: '店铺货款',
            onTap: () => Navigator.of(context).pushNamed('/account', arguments: 'store'),
          ),
          MineFuncButton(
            name: 'assets/images/store/ic_func_ad.png',
            title: '广告位',
            onTap: () {},
          ),
          MineFuncButton(
            name: 'assets/images/store/ic_func_qr_code.png',
            title: '店铺二维码',
            onTap: () {},
          ),
        ],
      ),
    );
  }
}
